package org.tabularium.text.se;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

import org.tabularium.text.TermFreq;
import org.tabularium.text.TermWeightVector;

import com.yahoo.search.*;

/**
 * Inoperative service.
 * 
 * Query Yahoo search engine using the Yahoo apis.
 */
public class YahooSearchEngine implements SearchEngine {
	public static final String DEFAULT_APPID = "IPWB6oPV34FQrToWY53jWItkbvAOYWhFA6fDDkFxmiLtxiQmi6sPNy3vgtpjCOh";
	// yahoo apis limits to 50 hits
	// it is possible to perform a further query to retrieve more than 50 results
	public static final int MAX_HITS = 50;
	protected SearchClient client = null;
	protected boolean htmlFilter = false;
	
	protected Logger logger = Logger.getLogger("fceval.backcrawler.YahooBackwardWebVisitor");
	
	public YahooSearchEngine() {
		this(DEFAULT_APPID);
	}

	public YahooSearchEngine(String appid) {
	    // Create the search client. Pass it your application ID.
		client = new SearchClient(appid);	
	}
	
	public void enableHtmlFilter(boolean on) {
		htmlFilter = on;
	}
	
	public HitsIterator query(String query) throws Exception {
	    // Create the web search request. In this case we're searching for
	    // java-related hits.
	    WebSearchRequest request = new WebSearchRequest(query);
	    request.setResults(MAX_HITS);
	    YahooHitsIterator hits = null;
	    if (htmlFilter) {
	    	request.setFormat("html");
	    }
	    try {
	        // Execute the search.
	        WebSearchResults results = client.webSearch(request);
	        hits = new YahooHitsIterator(results.listResults()); 
	    }
	    catch (IOException e) {
	    	logger.severe("Error calling Yahoo! Search Service: " +
	                e.toString());
	        e.printStackTrace(System.err);
	    }
	    catch (SearchException e) {
	    	logger.severe("Error calling Yahoo! Search Service: " +
	                e.toString());
	        // An issue with the XML or with the service.
	        e.printStackTrace(System.err);
	    }
		return hits;
	}
	


	public void index(String id, String doc) throws IOException {
		// TODO Auto-generated method stub

	}


	public void clear() throws IOException {
		// TODO Auto-generated method stub

	}

	public int numDocs() throws IOException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String[] lexicon() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public TermFreq[] docFreqs() throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isIndexed(String id) throws IOException {
		// TODO Auto-generated method stub
		return false;
	}

	public TermWeightVector termFreqs(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int getMaxClauseCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	
	public static void main(String[] args) throws Exception {
		YahooSearchEngine v = new YahooSearchEngine();
		HitsIterator iter = v.query("google");
		int i=0;
		while (iter.hasNext()) {
			System.out.println(++i+""+iter.next());
		}
	}


}

